package com.ngm.tea.t.service.impl;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.ngm.tea.common.PageInfo;
import com.ngm.tea.t.dao.RolePrivilegeDao;
import com.ngm.tea.t.entity.RolePrivilege;
import com.ngm.tea.t.service.RolePrivilegeService;

/**
 * 服务层
 * @author zjw
 * @version 1.0
 * @date 2016-09-01 21:07:07
 *
 */
@Service("rolePrivilegeService")
public class RolePrivilegeServiceImpl implements RolePrivilegeService {
	
	@Resource
	private RolePrivilegeDao rolePrivilegeDao;

	public RolePrivilegeDao getRolePrivilegeDao() {
		return rolePrivilegeDao;
	}

	@Override
	public void insert(RolePrivilege rolePrivilege) {
		rolePrivilegeDao.insert(rolePrivilege);
	}

	@Override
	public void deleteById(Long id) {
		rolePrivilegeDao.deleteById(id);
	}

	@Override
	public void deleteByIds(String ids) {
		String[] _ids = ids.split(",");
		if (_ids.length > 0) {
			List<String> idlist = Arrays.asList(_ids);
			rolePrivilegeDao.deleteByIds(idlist);
		}
	}

	@Override
	public void delete(RolePrivilege rolePrivilege) {
		rolePrivilegeDao.delete(rolePrivilege);
	}

	@Override
	public void update(RolePrivilege rolePrivilege) {
		rolePrivilegeDao.update(rolePrivilege);
	}

	@Override
	public RolePrivilege selectOne(Long id) {
		return rolePrivilegeDao.selectOne(id);
	}

	@Override
	public List<RolePrivilege> selectAll() {
		return rolePrivilegeDao.selectAll();
	}

	@Override
	public List<RolePrivilege> selectList(RolePrivilege rolePrivilege, PageInfo pageInfo) {
		Map map = new HashMap<String, Object>();
		map.put("searchParams",makeStr(rolePrivilege));
		map.put("pageInfo", pageInfo);
		map.put("order", pageInfo.getOrderField()+" "+pageInfo.getOrderDirection());
		return rolePrivilegeDao.selectList(map);
	}

	@Override
	public Long count(RolePrivilege rolePrivilege) {
		Map map = new HashMap<String, Object>();
		map.put("searchParams",makeStr(rolePrivilege));
		return rolePrivilegeDao.count(map);
	}
	
	public String makeStr(RolePrivilege rolePrivilege) {
		StringBuffer sb = new StringBuffer();
		if(!StringUtils.isEmpty(rolePrivilege.getId())) {
			if (StringUtils.isEmpty(sb.toString())) {
				sb.append("id LIKE '%" + rolePrivilege.getId()+"%'");
			} else {
				sb.append(" AND id LIKE '%" + rolePrivilege.getId()+"%'");
			}
		}
		if(!StringUtils.isEmpty(rolePrivilege.getRoleCode())) {
			if (StringUtils.isEmpty(sb.toString())) {
				sb.append("roleCode LIKE '%" + rolePrivilege.getRoleCode()+"%'");
			} else {
				sb.append(" AND roleCode LIKE '%" + rolePrivilege.getRoleCode()+"%'");
			}
		}
		if(!StringUtils.isEmpty(rolePrivilege.getMpId())) {
			if (StringUtils.isEmpty(sb.toString())) {
				sb.append("mpId LIKE '%" + rolePrivilege.getMpId()+"%'");
			} else {
				sb.append(" AND mpId LIKE '%" + rolePrivilege.getMpId()+"%'");
			}
		}
		if (StringUtils.isEmpty(sb.toString())) {
			return null;
		} else {
			return sb.toString();
		}
	}

	@Override
	public List<Map<String,Object>> selectOneByRoleCode(String roleCode) {
		Map<String,String> map = new HashMap<>();
		map.put("roleCode", roleCode);
		return rolePrivilegeDao.selectOneByRoleCode(map);
	}
	
}